/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月29日         luwei
 */

package com.coocaa.launcher.framework.sidebar;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.widget.LinearLayout;

import com.coocaa.launcher.framework.sidebar.item.ItemFactory;
import com.coocaa.launcher.framework.sidebar.item.ItemFactory.ITEM;
import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.launcher.framework.sidebar.item.LayoutVisibleCallBack;
import com.coocaa.launcher.pattern.normal.hotseat.FocusView;
import com.coocaa.launcher.sidebar.ParseredSetting;
import com.coocaa.launcher.sidebar.ReadXMLParser;
import com.coocaa.launcher.sidebar.SettingData;
import com.coocaa.launcher.sidebar.setting.layout.wallpaper.OnKeyTurnPageListener;
import com.coocaa.x.app.framework.utils.ConfigUtils;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.utils.SkyConstants;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public abstract class ItemContainer implements LayoutVisibleCallBack, OnEventListener {
    protected ContainerLayout mLayout = null;
    protected Context mContext = null;
    private OnLayoutVisibleChangedListener mVisibleChangedListener = null;
    private FocusView mFocusView = null;
    private List<IItem> mItems = new ArrayList<ItemContainer.IItem>();
    private int mShadowY = 0;
    protected String mType = "";
    protected static ParseredSetting mParseredSetting = null;

    public ItemContainer(Context context, String type) {
        mShadowY = SkyConstants.Div(14);
        this.mType = type;
        this.mContext = context;
        while (mParseredSetting == null) {
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        mLayout = createLayout();
        if (mLayout == null) {
            throw new RuntimeException("create layout is null!");
        }
        addItems();
    }

    public static void parseSettingXml(final Context context) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (mParseredSetting == null) {
                        mParseredSetting = ReadXMLParser.parserXML(ConfigUtils.getCurrnetConfig("launcher_setting.xml", context));
                    }
                } catch (Exception e) {
                    try {
                        mParseredSetting = ReadXMLParser.parserXML(context.getAssets().open("config/common/launcher_setting.xml"));
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    e.printStackTrace();
                }
            }
        }).start();

    }

    public void setLayoutVisibleChangedListener(OnLayoutVisibleChangedListener listener) {
        this.mVisibleChangedListener = listener;
    }

    private void addItems() {
        List<SettingData> settingDatas = getSettingDatas();
        if (settingDatas.size() == 1) {
            mLayout.showFocusView(false);
        }
        for (SettingData data : settingDatas) {
            IItem item = ItemFactory.createItem(ITEM.valueOf(data.type), mContext);
            if (settingDatas.size() == 1) {
                item.getView().setFocusable(false);
                item.getView().setFocusableInTouchMode(false);
            }
            if (item != null) {
                addItem(item);
                item.setType(ITEM.valueOf(data.type));
                item.setData(data);
                item.setExtraViewListener(mLayout);
                item.setLayoutVisibleCallBack(this);
                item.setEventListener(this);
                mItems.add(item);
            }
        }
    }

    public List<IItem> getItems() {
        return mItems;
    }

    private void addItem(IItem itemView) {
        if (itemView.getView().getLayoutParams() == null) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params.topMargin = SkyConstants.Div(15);
            itemView.getView().setLayoutParams(params);
        }
        mLayout.getContainerLayout().addView(itemView.getView());
    }

    public void removeAllItems() {
        mLayout.getContainerLayout().removeAllViews();
        mItems.clear();
    }

    public View getLayout() {
        return mLayout;
    }

    @Override
    public void showLayout() {
        mLayout.show();
        if (mVisibleChangedListener != null) {
            mVisibleChangedListener.visibleStateChanged(true);
        }
    }

    @Override
    public void hideLayout() {
        mLayout.hide();
        if (mVisibleChangedListener != null) {
            mVisibleChangedListener.visibleStateChanged(false);
        }
    }

    public boolean isLayoutVisible() {
        if (mLayout.getVisibility() == View.VISIBLE) {
            return true;
        }
        return false;
    }

    public abstract ContainerLayout createLayout();

    public abstract List<SettingData> getSettingDatas();

    public interface IItem {
        public View getView();

        public void setType(ITEM type);

        public ITEM getType();

        public ItemView createItemView();

        public void setExtraViewListener(OnExtraViewListener listener);

        public void setLayoutVisibleCallBack(LayoutVisibleCallBack callBack);

        public void setEventListener(OnEventListener listener);

        public void setIndex(int index);

        public int getIndex();

        public void destory();

        public void setKeyTurnPageListener(OnKeyTurnPageListener listener);

        public void setTurnPage(boolean[] turnPage);

        public void setData(Object object);
    }

    public interface OnExtraViewListener {
        public void addExtraView(LayoutParams params, View view, Animation sideAnim);

        public void removeExtraVeiw(View view, Animation sideAnim);
    }

    public interface OnLayoutVisibleChangedListener {
        public void visibleStateChanged(boolean show);
    }


    @Override
    public void onFocusChange(IItem item, boolean hasFocus) {
        if (hasFocus) {
            if (mFocusView == null) {
                mFocusView = mLayout.getFocusView();
            }
            if (mFocusView == null) {
                return;
            }
            View view = item.getView();
            int[] position = new int[2];
            view.getLocationInWindow(position);
            LogUtils.d("the top is " + position[0] + " and the left is " + position[1]);
            mFocusView.changeFocusPos(position[0], position[1] - mShadowY, view.getWidth(), view.getHeight() + 2 * mShadowY);
        }
    }

    public void destory() {
        for (IItem item : mItems) {
            item.destory();
        }
        mLayout.destory();
    }

    @Override
    public void onClick(IItem item, int btnIndex) {

    }
}
